﻿using System;
using Gp.Input;
using Gp.Scripts.Core;
using QFramework;
using TMPro;
using UnityEngine;
using UnityEngine.InputSystem;

namespace Gp.Scripts.GUI {
    public class ClickConformIndicator : MonoBehaviour, IController {
        public CanvasGroup indicator;
        public TMP_Text text;

        private Action _onClick;
        private Action _onCancel;

        private const string Tips1 = "点击确认"; 
        private const string Tips2 = "点击空白处确认"; 

        private void Start() {
            // if (Global.Get<GridBattleGUISystem>().ClickConformIndicator == null)
                // Global.Get<GridBattleGUISystem>().ClickConformIndicator = this;

            Clear();
        }

        private void LateUpdate() {
            if (_onClick == null) return;
            
            transform.position = Global.Get<InputManager>().MousePosition();

            if (Global.Get<InputManager>().CancelButtonDown() && _onCancel != null) {
                _onCancel();
                Clear();
                return;
            }


            if (CursorManager.IsPointerOverUIObject()) {
                text.text = Tips2;
                return;
            }


            text.text = Tips1;

            if (Mouse.current.leftButton.wasPressedThisFrame) {
                _onClick();
                Clear();
                return;
            }
            

            
        }


        private bool _block = false;
        public void Show(Action onClick, Action onCancel) {
            _onClick = onClick;
            _onCancel = onCancel;
            _block = true;
            indicator.alpha = 1f;
        }

        public void Clear() {
            _onClick = null;
            _onCancel = null;
            indicator.alpha = 0f;
        }
        
        
        public IArchitecture GetArchitecture() {
            return MainArchitecture.Interface;
        }
    }
}